package cn.jinjimi.app.custom.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.base.util.DateUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceWhenTrue;
import cn.jinjimi.app.R;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.custom.event.ClueAddCustomSucEvent;
import cn.jinjimi.app.custom.event.CustomChangeEvent;
import cn.jinjimi.app.custom.ui.view.CustomClueSaveMenuWindow;
import cn.jinjimi.framework.router.Router;
import cn.jinjimi.app.store.DatabaseManager;
import cn.jinjimi.app.store.entity.ClueEntity;
import cn.jinjimi.app.main.extend.ToolBarCommonActivity;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Roy
 * Date: 17/1/6
 */
public class CustomClueDetailActivity extends ToolBarCommonActivity {

    private TextView tvName;
    private TextView tvPhone;
    private TextView tvSource;
    private TextView tvSave;

    private TextView tvDate;
    private TextView tvTime;
    private TextView tvSecDate;
    private TextView tvEvent;
    private TextView tvContent;
    private TextView tvAdvice;
    private FrescoImageView imgIcon;

    private TextView btnCustom;

    private ClueEntity mClue;

    private CustomClueSaveMenuWindow mPopupWindow;

    public static void launch(Context startActivity, ClueEntity clue) {
        if (startActivity == null || clue == null)
            return;
        Intent intent = new Intent(startActivity, CustomClueDetailActivity.class);
        intent.putExtra(BundleKey.PARAM_CLUE, clue);
        startActivity.startActivity(intent);
    }

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_custom_clue_detail);
        mClue = (ClueEntity) getIntent().getSerializableExtra(BundleKey.PARAM_CLUE);

        setTitle(R.string.customer_clue_detail_title);
    }

    @Override
    protected void initial() {
        initView();
        initListener();
        initData();
    }

    private void initView() {
        tvName = (TextView) findViewById(R.id.tv_name);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        tvSave = (TextView) findViewById(R.id.tv_save);
        tvSource = (TextView) findViewById(R.id.tv_sorce);

        tvDate = (TextView) findViewById(R.id.tv_date);
        tvSecDate = (TextView) findViewById(R.id.tv_sec_date);
        tvTime = (TextView) findViewById(R.id.tv_time);
        tvEvent = (TextView) findViewById(R.id.tv_event);
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvAdvice = (TextView) findViewById(R.id.tv_advice);
        imgIcon = (FrescoImageView) findViewById(R.id.img_icon);

        btnCustom = (TextView) findViewById(R.id.btn_custom);
    }

    private void initListener() {
        findViewById(R.id.ll_sms).setOnClickListener(this);
        findViewById(R.id.ll_call).setOnClickListener(this);
        findViewById(R.id.ll_content).setOnClickListener(this);

        btnCustom.setOnClickListener(this);
    }

    private void initData() {
        if (mClue != null) {
            tvName.setText(StringUtils.isEmpty(mClue.realName) ? mClue.phone : mClue.realName);
            tvPhone.setText(mClue.phone);

            if (StringUtils.isEmpty(mClue.realName) && StringUtils.isEmpty(mClue.phone)) {
                tvName.setText(getString(R.string.clue_unknow_name));
            }

            if (StringUtils.isEmpty(mClue.realName) || StringUtils.isEmpty(mClue.phone)) {
                tvPhone.setVisibility(View.GONE);
            }

            tvSave.setVisibility(mClue.customerId == null ? View.VISIBLE : View.GONE);
            tvSource.setText(String.format(getString(R.string.clue_source), mClue.source));

            tvDate.setText(DateUtils.dealWithDataInCustomDay(mClue.createdTime));
            tvTime.setText(DateUtils.formatHHmm(mClue.createdTime));
            tvSecDate.setText(DateUtils.formatMonthYear(mClue.createdTime));
            tvEvent.setText(mClue.event);
            tvContent.setText(mClue.content);
            tvAdvice.setText(mClue.advice);
            tvAdvice.setVisibility(StringUtils.isEmpty(mClue.advice) ? View.GONE : View.VISIBLE);

            btnCustom.setText(mClue.customerId == null ? getString(R.string.clue_save_custom) : getString(R.string.clue_check_custom));

            findViewById(R.id.ll_sms).setVisibility(StringUtils.isEmpty(mClue.phone) ? View.GONE : View.VISIBLE);
            findViewById(R.id.ll_call).setVisibility(StringUtils.isEmpty(mClue.phone) ? View.GONE : View.VISIBLE);

            switch (mClue.category) {
                case ClueEntity.CategoryPolicy:
                    imgIcon.setPlaceHolderImage(R.drawable.client_file_icon_policy);
                    break;
                case ClueEntity.CategoryRedPack:
                    imgIcon.setPlaceHolderImage(R.drawable.client_file_icon_packet);
                    break;
                case ClueEntity.CategoryProposal:
                    imgIcon.setPlaceHolderImage(R.drawable.client_file_icon_project);
                    break;
                default:
                    imgIcon.setPlaceHolderImage(R.drawable.client_file_icon_default);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_sms: // 底部短信
                makeMsg();
                break;
            case R.id.ll_call: // 底部电话
                makeCall();
                break;
            case R.id.ll_content: // 线索链接
                openClueDetail();
                break;
            case R.id.btn_custom:
                if (mClue.customerId == null)
                    showAddCustomView();
                else {
                    openAchievesAty();
                }
                break;
        }
    }

    @EventTrace(EventID.APP_CUSTOMER_LEADDETAIL_VIEWFILE)
    private void openAchievesAty() {
        CustomArchivesActivity.launch(CustomClueDetailActivity.this, mClue.customerId == null ? 0 : Long.valueOf(mClue.customerId));
    }

    /* 发短信 */
    @EventTrace(EventID.APP_CUSTOMER_LEADDETAIL_SMS)
    public void makeMsg() {
        AndroidUtils.sendMsg(CustomClueDetailActivity.this, mClue.phone, "");
    }

    /* 打电话 */
    @EventTrace(EventID.APP_CUSTOMER_LEADDETAIL_PHONE)
    public void makeCall() {
        AndroidUtils.call(CustomClueDetailActivity.this, mClue.phone);
    }

    /* 打开线索详情链接 */
    public void openClueDetail() {
        Router.create(CustomClueDetailActivity.this, mClue.linkUrl).open();
    }

    /* 弹出新增客户和关联客户的按钮View */
    @EventTrace(EventID.APP_CUSTOMER_LEADDETAIL_SAVECLICK)
    private void showAddCustomView() {

        if (mPopupWindow == null) {
            mPopupWindow = new CustomClueSaveMenuWindow(this, mClue);
        }
        mPopupWindow.showAtLocation(getContentView(), Gravity.NO_GRAVITY, 0, 0);
        mPopupWindow.beginAnimate();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(ClueAddCustomSucEvent event) {
        shouldUpdateAndFinish(mClue.clueId.equals(event.clue.clueId), event);
    }

    @EventTrace(EventID.APP_CUSTOMER_EDITDETAIL_ADDSUCCESS)
    private void shouldUpdateAndFinish(@EventTraceWhenTrue boolean update, ClueAddCustomSucEvent event) {
        if (update) {
            this.mClue = event.clue;
            btnCustom.setText(getString(R.string.clue_check_custom));
            DatabaseManager.getInstance().getClueEntityDao().insertOrReplace(event.clue);
            DatabaseManager.getInstance().getCustomerDao().insertOrUpdate(event.custom);
            finish();
        }
    }

    /* 监听客户资料的变化 */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCustomChangeEvent(CustomChangeEvent event) {
        if (event.changeEvent == CustomChangeEvent.EVENT_DELETE
                && mClue.customerId.equals(String.valueOf(event.data.customerId))) {
            this.mClue.customerId = null;
            btnCustom.setText(getString(R.string.clue_save_custom));
        }
    }

}